package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/10 9:39
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category, HttpSession session) {
        //判断类别有没有重复
        Category cate = categoryMapper.findByName(category.getName());

        if (cate!=null){
            throw new BusinessException("类名已存在!");
        }

        //完善数据
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Long id = (Long) session.getAttribute("employee");

        category.setCreateUser(id);
        category.setUpdateUser(id);


        //调用Mapper新增方法
        return categoryMapper.save(category)>0;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public Page<Category> findAll(Integer page,Integer pageSize) {
        //调用方法计算start
        PageHelper.startPage(page,pageSize);

        //查询所有菜品和套餐的集合
        List<Category> categoryList = categoryMapper.findAll();

        //调用工具方法进行分页
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        //封装集合和总数
        Page<Category> categoryPage = new Page<>();
        categoryPage.setRecords(pageInfo.getList());
        categoryPage.setTotal(pageInfo.getTotal());
        return categoryPage;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean DeleteById(Long id) {
        Integer dishCount = dishMapper.dishCount(id);
        Integer setmealCount = setmealMapper.setmealCount(id);

        //如果菜品数量大于0，说明这个菜在菜品有分类，不能删
        if (dishCount>0){
            throw new BusinessException("该菜系已存在菜品，不能删除");
        }
        if (setmealCount>0){
            throw new BusinessException("该菜系已存在套餐，不能删除");
        }

        return categoryMapper.delete(id)>0;
    }

    /**
     * 通过id查找，回显
     *
     * @param id
     * @return
     */
    @Override
    public Category findById(Long id) {
        Category category = categoryMapper.findById(id);
        return category;
    }

    /**
     * 更改
     *
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category,HttpSession session) {
        //判断更改的类别名是否存在
        Category cateByName = categoryMapper.findByName(category.getName());
        if (cateByName!=null && !category.getId().equals(cateByName.getId())){
            throw new BusinessException("类别已存在");
        }

        //完善信息
        category.setUpdateTime(LocalDateTime.now());

        Long id = (Long) session.getAttribute("employee");
        category.setUpdateUser(id);

        //调用mapper层更新方法
        return categoryMapper.update(category)>0;
    }

    public List<Category> findAll(){
       return categoryMapper.findAll();
    }

    /**
     * 根据type查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findByType(Integer type) {
        return categoryMapper.findByType(type);
    }
}
